package com.usian.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.admin.mapper.AdUserMapper;
import com.usian.admin.service.AdUserService;
import com.usian.model.admin.dtos.AdUserDto;
import com.usian.model.admin.pojos.AdSensitive;
import com.usian.model.admin.pojos.AdUser;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.utils.common.AppJwtUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.*;

@Service
public class AdUserServiceimpl extends ServiceImpl<AdUserMapper, AdUser>
        implements AdUserService {


    @Override
    public ResponseResult login(AdUserDto adUserDto) {
        //1:参数校验
        if(StringUtils.isBlank(adUserDto.getName())||StringUtils.isBlank(adUserDto.getPassword())){
            return  ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //2:生成token
        QueryWrapper<AdUser> wrapper =new QueryWrapper<>();
        wrapper.lambda().eq(AdUser::getName,adUserDto.getName());

        List<AdUser> userList = list(wrapper);
        if(userList !=null && userList.size()==1 ){
            AdUser user = userList.get(0);

            //用户输入的密码 +数据库的盐值  重新生成一个pws 的加密串
            String pwd = DigestUtils.md5DigestAsHex(( adUserDto.getPassword()+ user.getSalt()).getBytes());

            if(user.getPassword().equals(pwd)){
                //输入的密码一样   token
                //通过jwt  生成token
                String token = AppJwtUtil.getToken(user.getId().longValue());

                HashMap<String,Object> map =new HashMap<>();
                map.put("token",token);
                user.setPassword("");
                user.setSalt("");
                map.put("user",user);
                return ResponseResult.okResult(map);

            }else{
                return  ResponseResult.errorResult(AppHttpCodeEnum.LOGIN_PASSWORD_ERROR);
            }
        }else{
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
        }
    }

    public static void main(String[] args) {

        try {
            List<String> list = new ArrayList<>();
            list.add("11111");
            list.add("22222");
            list.add("33333");

            System.out.println(list.size());

            /*for (int i=0;i<list.size();i++){
                if(list.get(i)=="22222"){
                    list.remove(i);
                }
            }*/

           /* for(String s :list){
                if(s=="22222"){
                    list.remove(s);
                }
            }*/

            Iterator<String> iterator= list.iterator();
            while (iterator.hasNext()){
                if(iterator.next()=="22222"){
                    iterator.remove();
                }
            }

            System.out.println(list.size());
        }catch (Exception e){
            e.printStackTrace();
        }




    }
}
